﻿Public Class CoreEventHandler

    ' Top of Main Thread stack says: [In sleep, wait or join]

    Private m_ignoreevents As Boolean
    Private WithEvents m_core As CoreEventSource
    Private m_originator As Boolean

    Const WRITELOG As Boolean = False

    Public Sub New()
        m_ignoreevents = False
        m_originator = False
        m_core = SpatialAlbum.Core.CoreEventSource.GetEventSource
    End Sub

    'Public Property IgnoreEvents() As Boolean
    '    Get
    '        Return m_ignoreevents
    '    End Get
    '    Set(ByVal value As Boolean)
    '        m_ignoreevents = value
    '    End Set
    'End Property

    Public Property Originator() As Boolean
        Get
            Return m_originator
        End Get
        Set(ByVal value As Boolean)
            m_originator = value
        End Set
    End Property

    Private Sub m_core_Debug(ByVal msg As String) Handles m_core.Debug
        Write("Debug", msg)
        Originator = True
        If Not m_ignoreevents Then Debug.WriteLine(msg)
        Originator = False
    End Sub

    Private Sub m_core_Trace(ByVal msg As String) Handles m_core.Trace
        Write("Trace", msg)
        Originator = True
        If Not m_ignoreevents Then Trace.WriteLine(msg)
        Originator = False
    End Sub

    Public Sub LogListener(ByVal msg As String)
        If Originator Then Return
        Write("Trace (LogListener)", msg)
    End Sub

    Private Sub m_core_Error(ByVal msg As String) Handles m_core.Error
        Write("Error", msg)
        If Not m_ignoreevents Then ReportingUtils.ReportError(msg)
    End Sub

    Private Sub m_core_Exception(ByVal ex As System.Exception) Handles m_core.Exception
        Write("Exception", ex)
        ReportingUtils.ReportException(ex)
    End Sub

    Private Sub m_core_ShowTimer(ByVal msg As String) Handles m_core.ShowTimer
        Write("ShowTimer")
        ReportingUtils.ShowTimer(msg)
    End Sub

    Private Sub m_core_SqlEvent(ByVal e As Core.SqlEventArgs) Handles m_core.SqlEvent
        Write("SqlEvent", e)
        ReportingUtils.ReportSqlEvent(e)
    End Sub

    Private Sub m_core_StartTimer() Handles m_core.StartTimer
        ReportingUtils.StartTimer()
    End Sub

    Dim m_of As System.IO.StreamWriter
    Dim m_logfile As String = ""

    Public Sub Write(ByVal key As String, Optional ByVal msg As String = "")
        If Not WRITELOG Then Return
        OpenFile()
        m_of.WriteLine("{0,5}: {1:10} - {2}", key, m_ignoreevents.ToString, msg)
        CloseFile()
    End Sub
    Private Sub Write(ByVal key As String, ByVal ex As System.Exception)
        If Not WRITELOG Then Return
        OpenFile()
        m_of.WriteLine("{0,5}: {1:10} - {2}", key, m_ignoreevents.ToString, ex.ToString)
        CloseFile()
    End Sub
    Private Sub Write(ByVal key As String, ByVal e As Core.SqlEventArgs)
        If Not WRITELOG Then Return
        OpenFile()
        m_of.WriteLine("{0,5}: {1:10} - {2} - {3}", key, m_ignoreevents.ToString, e.Name, e.Message)
        CloseFile()
    End Sub
    Private Sub OpenFile()
        If Not WRITELOG Then Return
        If m_logfile = "" Then m_logfile = "C:\logfile.txt"
        m_of = New System.IO.StreamWriter(m_logfile, True)
    End Sub
    Private Sub CloseFile()
        If Not WRITELOG Then Return
        m_of.Close()
    End Sub
End Class
